package com.gemalto.Kryptik.Util;

import javax.swing.*;
import java.net.URL;

public class Util {

	private static IniFiles ini;


	public static String omitSpaces(String in){
		String out="";
		for (int i=0; i<in.length(); i++){
	     		if ( in.charAt(i)!=' ' ) out+=in.charAt(i);
	   	}
	   	return out;
	 }



	public static void loadIni(String filename){
		ini = new IniFiles(filename);
		ini.loadIni();
	}


	public static IniFiles getIni(){
		return ini;
	}


	public static void saveIni(JFrame g){
		int x = (int)g.getLocation().getX();
		int y = (int)g.getLocation().getY();
		int w = (int)g.getSize().getWidth();
		int h = (int)g.getSize().getHeight();

		ini.setValue("GUI","X", x); //((int)g.getLocation().getX())):
		ini.setValue("GUI","Y", y); //((int)g.getLocation().getY())):
		ini.setValue("GUI","W", w);
		ini.setValue("GUI","H", h);
		ini.saveIni();
	}


	public static String getTranslation(String query){
	     String language = ini.getValue("GUI","LANGUAGE");
	     String trans=ini.getValue("TRANSLATION_"+language, query);

	     // Omit mnemonic 
	     // Remove '&' char which is not preceded by '\'
	     // '\&' replace by '&'
		 StringBuffer sb = new StringBuffer(trans);

	     for (int i=0; i< sb.length();i++){
		 	if (sb.charAt(i)=='&'){
				if (i>0 && sb.charAt(i-1)=='\\'){
					sb.deleteCharAt(i-1);
		 		} else {
					sb.deleteCharAt(i);
		 		}
		 	}
		 trans=sb.toString();
	     }	
	     return trans;
	}


	public static int getMnemonicChar(String query){
	    String language = ini.getValue("GUI","LANGUAGE");
	    String s=ini.getValue("TRANSLATION_"+language, query);
		int mnemonic=-1;
		int i=s.indexOf('&');

	    while (i>-1){
			if (i==0 || s.charAt(i-1)!='\\'){
				mnemonic=s.charAt(i+1);
				break;
			}
			i=s.indexOf('&',i);
		}	 
		return mnemonic;
	}



	public static ImageIcon getIcon(String name) {

             ImageIcon icon = null;
             URL iconURL = ClassLoader.getSystemResource("gifs/" + name);
             if (iconURL != null)
             icon = new ImageIcon(iconURL);
             return icon;
       }
}

// vim:tabstop=4:nowrap 
